package com.word.model;

import com.word.model.handler.Model1Handler;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;

@Component
public class ModelFactory {

    public static ModelFactory getInstance(){
        return instance;
    }

    private static ModelFactory instance;

    private Map<Short,Class<?>> modelMap;

    private ModelFactory(){}

    @PostConstruct
    public void init(){
        instance = new ModelFactory();
        instance.modelMap = new HashMap<>();
        instance.modelMap.put(ModelType.CONTRACT1, Model1Handler.class);
    }

    public ModelType produceModelType(Short type) throws NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
        return (ModelType) modelMap.get(type).getDeclaredConstructor().newInstance();
    }
}
